package com.karaku.karaku_collector.agora;

import com.karaku.karaku_collector.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.IRtcEngineEventHandlerEx;

/**
 * Created by guoping on 2017/10/26.
 */

public class CollectorRtcEventHandler extends IRtcEngineEventHandler {

    private static CollectorRtcEventHandler globalHandler = new CollectorRtcEventHandler();
    public static CollectorRtcEventHandler getGlobalHandler() {
        return globalHandler;
    }

    private final List<IRtcEngineEventHandler> handlers = new ArrayList<>();

    public void addSubHandler(IRtcEngineEventHandler handler) {
        synchronized (handlers) {
            if (!handlers.contains(handler)) {
                handlers.add(handler);
            }
        }
    }

    public void removeHandler(IRtcEngineEventHandler handler) {
        synchronized (handlers) {
            handlers.remove(handler);
        }
    }

    @Override
    public void onError(int err) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onError(err);
            }
        }
        needRestart();
        LogUtil.d("VIDEO", "onError:" + err);
    }

    @Override
    public void onUserOffline(int uid, int reason) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onUserOffline(uid, reason);
            }
        }
        LogUtil.d("VIDEO", "onUserOffline uid:" + uid + " reason:" + reason);
    }

    /**
     * 首次断连
     */
    @Override
    public void onConnectionInterrupted() {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onConnectionInterrupted();
            }
        }
        LogUtil.d("VIDEO", "onConnectionInterrupted");
    }

    /**
     * 重连失败(sdk 自动重试)
     */
    @Override
    public void onConnectionLost() {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onConnectionLost();
            }
        }
        LogUtil.d("VIDEO", "onConnectionLost");
    }

    @Override
    public void onWarning(int warn) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onWarning(warn);
            }
        }
        LogUtil.d("VIDEO", "onWarning:" + warn);
    }

    /**
     * 五秒内无uid用户消息
     * @param uid
     * @param streamId
     * @param error
     * @param missed
     * @param cached
     */
    @Override
    public void onStreamMessageError(int uid, int streamId, int error, int missed, int cached) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onStreamMessageError(uid, streamId, error, missed, cached);
            }
        }
        LogUtil.d("VIDEO", "onStreamMessageError");
    }

    @Override
    public void onRejoinChannelSuccess(String channel, int uid, int elapsed) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onRejoinChannelSuccess(channel, uid, elapsed);
            }
        }
        LogUtil.d("VIDEO", "onRejoinChannelSuccess channel:" + channel + " uid:" + uid + " elapsed:" + elapsed);
    }

    /**
     * channelKey过期
     */
    @Override
    public void onRequestChannelKey() {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onRequestChannelKey();
            }
        }
        LogUtil.d("VIDEO", "onRequestChannelKey");
        needRestart();
    }

    @Override
    public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onJoinChannelSuccess(channel, uid, elapsed);
            }
        }
        LogUtil.d("VIDEO", "onJoinChannelSuccess channel:" + channel + " uid:" + uid + " elapsed:" + elapsed);
    }

    @Override
    public void onLocalVideoStats(LocalVideoStats stats) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onLocalVideoStats(stats);
            }
        }
    }

    @Override
    public void onNetworkQuality(int uid, int txQuality, int rxQuality) {
        synchronized (handlers) {
            for (IRtcEngineEventHandler h : handlers) {
                h.onNetworkQuality(uid, txQuality, rxQuality);
            }
        }
        LogUtil.d("VIDEO", "onNetworkQuality uid:" + uid + " txQuality:" + txQuality + " rxQuality:" + rxQuality);
    }

    public void needRestart() {

    }
}
